package com.taofuxn.yycreazy.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.util.AppStyleUtil;
import com.taofuxn.yycreazy.util.MeasureUtils;
import com.taofuxn.yycreazy.util.MyLog;

/**
 * 主题选择的对话框
 * Created by taofuxn on 2016/9/23.
 */
public class ChooseThemeDialog {


    public int themeColor[] = {R.color.slateblue,R.color.black,R.color.yellow,R.color.palegreen,R.color.rosybrown,R.color.darkorchid};
    private AlertDialog mAlertDialog;

    private AlertDialog.Builder mBuilder;

    private AppStyleUtil mAppStyleUtil;

    public ChooseThemeDialog(final Context mContext){
        mBuilder = new AlertDialog.Builder(mContext);
        mBuilder.setTitle("更换主题");
        mAppStyleUtil =  new AppStyleUtil<>();
        View v  = LayoutInflater.from(mContext).inflate(R.layout.dialog_switch_theme,null,false);
        XCFlowLayout container = (XCFlowLayout) v.findViewById(R.id.themeContainer);

        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int width = MeasureUtils.getScreenWidth(mContext);
        lp.width = width / 10;
        lp.height = width /10;
        lp.leftMargin = (width - lp.width*3) / 5;
        lp.rightMargin = lp.leftMargin / 4;
        lp.topMargin =  width / 5 / 3;
        for (int i = 0 ; i< themeColor.length;i++){
            TextView colorTv = new TextView(mContext);
            colorTv.setBackgroundDrawable(getDrawable( mContext.getResources().getColor(themeColor[i])));
            container.addView(colorTv,lp);
            colorTv.setTag(AppStyleUtil.themeSum[i]);
            colorTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAppStyleUtil.switchThemeTo((AppStyleUtil.StyleType) v.getTag(), (AppCompatActivity) mContext);
                    dismiss();
                }
            });
        }
        mBuilder.setView(v);
        mAlertDialog = mBuilder.create();
    }

    public void show(){
        mAlertDialog.show();
    }

    public void dismiss(){
        mAlertDialog.dismiss();
    }



    /**
     * 创建一个以主题颜色相关的Drwable
     * @param color 主题色
     */
    public Drawable getDrawable(int color){
        GradientDrawable gd = new GradientDrawable();
        gd.setGradientType(GradientDrawable.OVAL);
        gd.setColor(color);
        gd.setCornerRadius(35);
        gd.setStroke(2,color);
        return gd;
    }

}
